/**
 * 工程名: 	Quickdisk
 * 文件名: 	UpDown.java
 * 创建人:  	guo.tianxiang
 * 创建时间: 	2014-4-13 下午1:23:18
 * 版权所有：	郭天翔
 * 文件描述: 描述该文件的作用
 * -----------------------------变更记录 ----------------------------- 
 * 日期        		变更人      		版本号  		变更描述  
 * ------------------------------------------------------------------  
 * 2014-4-13     guo.tianxiang   	1.0       	first created  
 */
package com.tianxiang.action;

import java.io.File;
import java.text.ParseException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.model.MyFile;
import com.tianxiang.model.User;
import com.tianxiang.service.ManageFileService;
import com.tianxiang.service.UpDownService;
import com.tianxiang.utils.Tools;

/**
 * 处理上传的Action请求
 * 
 * @since Quickdisk 0.1
 * @version 1.0 2014-4-13
 * @author guo.tianxiang
 */
public class UpLoadFileAction extends ActionSupport{
	/**
	* serialVersionUID: (用一句话描述这个变量表示什么)
	*/
	private static final long serialVersionUID = 1L;
	private String fileCode;
	private File upFile; // 表单传递的文件域
	private String savePath; // 页面传来保存文件的位置
	private String upFileContentType; // 用于获取上传文件的类型
	private String upFileFileName; // 用于获取上传文件的实际名称
	private String msg; // 用于返回到页面的处理结果反馈
	private boolean success;	//用于表示上传是否成功
	private float usedspace;   //实时记录已用的空间
	UpDownService upDownService = null;
	ManageFileService manageFileService = null;
	@JSON(serialize = false)
	public File getUpFile() {
		return upFile;
	}
	
	@JSON(serialize = false)
	public String getFileCode() {
		return fileCode;
	}


	public void setFileCode(String fileCode) {
		this.fileCode = fileCode;
	}


	public void setUpFile(File upFile) {
		this.upFile = upFile;
	}


	@JSON(serialize = false)
	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	@JSON(serialize = false)
	public String getUpFileContentType() {
		return upFileContentType;
	}

	public void setUpFileContentType(String upFileContentType) {
		this.upFileContentType = upFileContentType;
	}

	@JSON(serialize = false)
	public String getUpFileFileName() {
		return upFileFileName;
	}

	public void setUpFileFileName(String upFileFileName) {
		this.upFileFileName = upFileFileName;
	}

	public String getMsg() {
		return msg;
	}

	public boolean getSuccess() {
		return success;
	}

	public float getUsedspace() {
		return usedspace;
	}

	public String upLoad() throws ParseException {
			User user=(User)ServletActionContext.getRequest().getSession()
					.getAttribute("loginuser");
	String username=user.getUsername();
	int userid=user.getId();
		upDownService = new UpDownService();
		manageFileService=new ManageFileService();
		String destPath = Tools.getAcessPath(username,"", savePath); // 实际要保存的位置
		System.out.println("保存在：" + destPath);
		File objFile = new File(Tools.getPublicPath(), getUpFileFileName());
		System.out.println("上传的文件是:" + getUpFileFileName());
		System.out.println("上传的文件类型是:" + getUpFileContentType());

		String code=fileCode;
		int result=-1;
		boolean flag=false;
		System.out.println("code-->"+code);
		MyFile mf=manageFileService.getAlreadyFileByCode(code);
		int fId=-1;
		if(mf!=null){
			fId=mf.getFid();
		}
		//int fId=manageFileService.fileAlreadyExist(getUpFile());
		if(fId!=-1){
			System.out.println("文件已经存在可以实现秒传");
			result=1;
			result=upDownService.uploadExistFile(userid,getUpFile().length(),fId);//上传已存在的文件
		}else{
			System.out.println("文件不存在将储备一份");
			if(upFile==null){
				System.out.println("kkkkkkkkkkkk");
				return "false";
			}
			if(manageFileService.saveFile(getUpFile(),getUpFileFileName(),
					Tools.getFileType(getUpFileFileName()),
					Tools.getPublicPath()+getUpFileFileName(),code)){
					System.out.println("保存文件信息成功");
					result=upDownService.upload(userid,upFile, objFile);//文件上传至public目录下
				}else{
					result=-1;
				}
			}
		//fId=manageFileService.fileAlreadyExist(getUpFile());
		fId=manageFileService.getAlreadyFileByCode(code).getFid();
		flag=manageFileService.saveOwner(username, getUpFileFileName(),
				 destPath+"\\"+getUpFileFileName(),Tools.getLastModefyTime(),fId);//保存单例文件信息
		
		usedspace=user.getUsedspace();
		if (result==0) {
			success=false;
			msg="空间不足";
		}else if (result==1&&flag) {
			success=true;
			msg="文件'"+getUpFileFileName()+"'成功上传至'"+savePath+"'！";
		}else {
			success=false;
			msg="文件上传失败，请重试！";
		}
		return SUCCESS;

	}

}
